package cn.edu.scau.cmi.ema.dao;

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.dao.DataAccessException;

import org.springframework.stereotype.Repository;

import org.springframework.transaction.annotation.Transactional;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.Session;


import cn.edu.scau.cmi.ema.domain.*;
import cn.edu.scau.cmi.ema.domain.base.CmiPagination;


import cn.edu.scau.cmi.ema.util.CmiSqlUtil;

@Repository("DetectdetailDAO")
@Transactional
public class DetectdetailDAOImpl extends AbstractJpaDao<Detectdetail> implements DetectdetailDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(
			Arrays.asList(new Class<?>[] { Detectdetail.class }));

	@PersistenceContext(unitName = "ema")
	private EntityManager entityManager;

	public DetectdetailDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Detectdetail findDetectdetailById(Integer id) throws DataAccessException {

		return findDetectdetailById(id, -1, -1);
	}

	@Transactional
	public Detectdetail findDetectdetailById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdetailById", startResult, maxRows, id);
			return (Detectdetail) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailByName(String name) throws DataAccessException {

		return findDetectdetailByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByName(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdetailByName", startResult, maxRows, name);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailByNameContaining(String name) throws DataAccessException {

		return findDetectdetailByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByNameContaining(String name, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdetailByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailByValue(String value) throws DataAccessException {

		return findDetectdetailByValue(value, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByValue(String value, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdetailByValue", startResult, maxRows, value);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailByValueContaining(String value) throws DataAccessException {

		return findDetectdetailByValueContaining(value, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByValueContaining(String value, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdetailByValueContaining", startResult, maxRows, value);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailByDescription(String description) throws DataAccessException {

		return findDetectdetailByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByDescription(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdetailByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailByDescriptionContaining(String description) throws DataAccessException {

		return findDetectdetailByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findDetectdetailByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@Transactional
	public Detectdetail findDetectdetailByPrimaryKey(Integer id) throws DataAccessException {
		return findDetectdetailByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Detectdetail findDetectdetailByPrimaryKey(Integer id, int startResult, int maxRows)
			throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdetailByPrimaryKey", startResult, maxRows, id);
			return (Detectdetail) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdetailsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Detectdetail> findAllDetectdetails() throws DataAccessException {

		return findAllDetectdetails(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findAllDetectdetails(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdetails", startResult, maxRows);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectdetail> findDetectdetailsByDetect(Integer detectId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdetailsByDetect", start, max, detectId);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectdetail> findDetectdetailsByFeature(Integer featureId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdetailsByFeature", start, max, featureId);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectdetail> findDetectdetailsByUnit(Integer unitId, int start, int max) throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdetailsByUnit", start, max, unitId);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	public Set<Detectdetail> findDetectdetailsByDetectmethod(Integer detectmethodId, int start, int max)
			throws DataAccessException {
		Query query = createNamedQuery("findAllDetectdetailsByDetectmethod", start, max, detectmethodId);
		return new LinkedHashSet<Detectdetail>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Detectdetail> findDetectdetailByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Detectdetail>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Detectdetail> findDetectdetailsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Detectdetail.class);
		Session session = entityManager.unwrap(Session.class);
		Set<Detectdetail> detectdetails = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return detectdetails;
	}

	@Transactional
	public Detectdetail findDetectdetailByPhysicalPrimaryKey(Integer detectId, Integer featureId, String value,
			Integer unitId) throws DataAccessException {
		return findDetectdetailByPhysicalPrimaryKey(detectId, featureId, value, unitId, -1, -1);
	}

	@Transactional
	public Detectdetail findDetectdetailByPhysicalPrimaryKey(Integer detectId, Integer featureId, String value,
			Integer unitId, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findDetectdetailByPhyscialPrimaryKey", startResult, maxRows, detectId,
					featureId, value, unitId);
			return (Detectdetail) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Detectdetail entity) {
		return true;
	}
}
